#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Motor.h"
#include "OLED.h"
#include "HC_SR04.h"

int main(void)
{
	Delay_ms(500);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	OLED_Init();
	HC_SR04Init();
	Motor_Init();

	OLED_ShowString(1,1,"Length:");
	OLED_ShowChar(1,11,'.');
	OLED_ShowString(1,14,"cm");
	while(1)
	{
		int Distance_mm = Sonar();
		int Distance_cm = Distance_mm / 10;
		int Distance_cm_p = Distance_mm % 10;
		OLED_ShowNum(1,8,Distance_cm,3);
		OLED_ShowNum(1,12,Distance_cm_p,1);
				
		if (Distance_cm ==0 && Distance_cm_p==0)
		{
			Car_Stop();
		}
		else
		{
			if (Distance_cm > 20)
			{
				Car_Forward(200);
			}
			else if (Distance_cm < 10)
			{
				Car_Backward(200);
			}
			else
			{
				Car_Stop();
			}
		}
		Delay_ms(300);
	}
}


